package com.gitee.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.bo.FoodBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.exception.FoodException;
import com.gitee.mapper.FoodMapper;
import com.gitee.pojo.Food;
import com.gitee.service.ICookbookFoodService;
import com.gitee.service.IFoodService;
import com.gitee.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements IFoodService {


    @Autowired
    private ICookbookFoodService cookbookFoodService;

    /**
     * 获取菜品
     *
     * @param keywords
     * @param pageNum
     * @param pageSize
     * @return {@link Food}
     */
    @Override
    public PageVo<Food> pageFoods(String keywords, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Food> lqw = new QueryWrapper<Food>().lambda()
                .eq(!StrUtil.isBlank(keywords), Food::getTitle, keywords)
                .orderByDesc(Food::getCreateTime);
        Page<Food> page = super.page(new Page<>(pageNum, pageSize), lqw);
        return new PageVo<>(page.getRecords(), (int)page.getTotal());
    }

    /**
     * 添加菜品
     *
     * @param foodBo
     * @return
     */
    @Transactional
    @Override
    public Boolean addFood(FoodBo foodBo) {
        LambdaQueryWrapper<Food> queryWrapper = new QueryWrapper<Food>().lambda().eq(Food::getTitle, foodBo.getTitle());
        if (super.count(queryWrapper) > 0) {
            throw new FoodException(ResponseEnum.FOOD_EXIST);
        }
        Food food = new Food();
        BeanUtils.copyProperties(foodBo, food);
        return super.save(food);
    }

    /**
     * 更新菜品
     *
     * @param foodBo
     * @param foodId
     * @return
     */
    @Transactional
    @Override
    public Boolean updateFood(FoodBo foodBo, Integer foodId) {
        LambdaQueryWrapper<Food> queryWrapper = new QueryWrapper<Food>().lambda()
                .eq(Food::getTitle, foodBo.getTitle())
                .ne(Food::getId, foodId);
        if (super.count(queryWrapper) > 0) {
            throw new FoodException(ResponseEnum.FOOD_EXIST);
        }
        Food food = new Food();
        BeanUtils.copyProperties(foodBo, food);
        food.setId(foodId);
        return super.updateById(food);
    }

    /**
     * 删除菜品
     * @param foodId
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteFood(Integer foodId) {
        return super.removeById(foodId) && cookbookFoodService.removeByFoodId(foodId);
    }

    /**
     * 批量删除菜品
     *
     * @param foodIds
     * @return
     */
    @Override
    public Boolean deleteFoodBatch(Integer[] foodIds) {
        return super.removeByIds(Arrays.asList(foodIds)) && cookbookFoodService.removeByFoodIds(foodIds);
    }
}
